//
//  LHReadingViewController.m
//  EPA
//
//  Created by 卢浩 on 16/4/18.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHReadingViewController.h"
#import "LHNetWorkRequestManager.h"
#import "LHHReadingCollectionViewCell.h"
#import "LHReadingModel.h"
#import "LHCycleScrollView.h"
#import "LHReadListController.h"
#import "LHReadingImgModel.h"

@interface LHReadingViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

//CollectionView
@property (nonatomic,strong) UICollectionView *collectionView;

//CollectionView数组属性
@property (nonatomic,strong) NSMutableArray *collectionData;

//轮播图数组
@property (nonatomic,strong) NSMutableArray *scrollData;

//轮播图
@property (nonatomic,strong) LHCycleScrollView *cycleView;

@end

@implementation LHReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"阅  读";
    
    self.collectionData = [NSMutableArray array];
    
    self.scrollData = [NSMutableArray array];
    
    [self loadCollectionData];
    
    //创建头轮播图
    self.cycleView = [[LHCycleScrollView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 220) Interval:5];
    self.cycleView.defaultImg = [UIImage imageNamed:@"图片正在加载.jpeg"];
    
    [self createCollectionView];
}

//创建CollectionView
- (void)createCollectionView{
    //设置layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width/3 - 2,self.view.frame.size.width/3 - 2);
    //设置Item的行间距(如果不设置,默认值为10)
    layout.minimumLineSpacing = 0;
    //设置Item的列间距
    layout.minimumInteritemSpacing = 0;
    //设置UICollectionView距离屏幕上,下,左,右的一个距离
    layout.sectionInset = UIEdgeInsetsMake(0, 3, 0, 3);
    //设置头部视图高度
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 260);
    //创建CollectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"LHHReadingCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"LHHReadingCollectionViewCell"];
    //注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
}

//获取数据
- (void)loadCollectionData{
    NSDictionary *paramsDict = @{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":[LHUserInfoManager shareManager].auth,@"version":@"3.0.2"};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:READLIST_URL] Methed:POST Params:paramsDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *readsDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = readsDict[@"data"];
            NSArray *listArr = dataDict[@"list"];
            NSMutableArray *readTemp = [NSMutableArray array];
            for (NSDictionary *readDict in listArr) {
                LHReadingModel *model = [[LHReadingModel alloc]init];
                [model setValuesForKeysWithDictionary:readDict];
                [readTemp addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                if (readTemp.count > 0) {
                    [self.collectionData removeAllObjects];
                    [self.collectionData addObjectsFromArray:readTemp];
                }
                [self.collectionView reloadData];
            });
            
            NSDictionary *carouselArr = dataDict[@"carousel"];
            //存放图片全部信息
            NSMutableArray *imgTemp = [NSMutableArray array];
            //单独放图片链接
            NSMutableArray *imgNameTemp = [NSMutableArray array];
            for (NSDictionary *imgDict in carouselArr) {
                LHReadingImgModel *model = [[LHReadingImgModel alloc]init];
                [model setValuesForKeysWithDictionary:imgDict];
                [imgTemp addObject:model];
                [imgNameTemp addObject:model.img];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                if (imgTemp.count > 0) {
                    [self.scrollData removeAllObjects];
                    [self.scrollData addObjectsFromArray:imgTemp];
                    self.cycleView.imgNameArray = imgNameTemp;
                }
            });
        }
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LHHReadingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LHHReadingCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.collectionData[indexPath.row];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        headerView.userInteractionEnabled = YES;
        [headerView addSubview:self.cycleView];
        return headerView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LHReadingModel *model = self.collectionData[indexPath.row];
    LHReadListController *readListVC = [[LHReadListController alloc]init];
    readListVC.typeID = model.type;
    readListVC.typeName = model.name;
    [self.navigationController pushViewController:readListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
